import type { UserInfo, Platform, Page, Cookie } from 'shared-types'
import { defineBean } from '../util'

function createStore() {
  let userInfo: UserInfo = {
    user_id: 0,
    name: '',
    avatar: '',
    role: 0
  }
  let platforms: Platform[] = []
  let pages: Page[] = []
  let cookies: Cookie[] = []

  function setUserInfo(val: Partial<UserInfo>) {
    userInfo = { ...userInfo, ...val }
  }
  function getUserInfo() {
    return userInfo
  }
  function isAdminRole() {
    return userInfo.role === 1
  }
  function setPlatforms(val: Platform[]) {
    platforms = val
  }
  function getPlatforms() {
    return platforms
  }
  function setPages(val: Page[]) {
    pages = val
  }
  function getPages() {
    return pages
  }
  function setCookies(val: Cookie[]) {
    cookies = val
  }
  function getCookies() {
    return cookies
  }

  return {
    setUserInfo,
    getUserInfo,
    isAdminRole,
    setPlatforms,
    getPlatforms,
    setPages,
    getPages,
    setCookies,
    getCookies
  }
}

export const useStore = defineBean(createStore)
